// pages/orderAppraise/appraise.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    img: {
      imageUploadTitle: "",
      sourceType: ["camera", "album"],
      sizeType: ["compressed"],
      maxCount: 9,
      uploadedImagesPaths: [],
      uploadParams: {
        url: "",//请求连接
        name: "file",
        formData: {
          filePath: "",
          name: "file"
        }
      }
    },
    orderId:0,
    uploadedImg: [],
    chooseImageData: [],
    starNum: 5,
    commentContent: "",
    orderinfo:[],
    infoid:0,
    order:[],
    shop:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options',options)
    this.data.orderId = parseInt(options.order_id)
    if (options.infoid){
      this.data.infoid = parseInt(options.infoid)
    }else{
      this.data.infoid = 0
    }
    console.log('infoid', this.data.infoid)
    this.onHttpInit()
  },
  /**
     * 获取页面数据的 http请求
     */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/product/order/detail', {
      order_id: that.data.orderId,
      info_id: that.data.infoid,
    }, function (res) {
      console.log("订单评论加载数据", res)
      if (res.data.code == 0) {
        console.log('infos',res.data.data.order.infos)
        that.setData({ 
          orderinfo: res.data.data.order != null ? res.data.data.order:[],
          shop:res.data.data.shop,
          order:res.data.data.order
        })
      }
    },function(){})
  },
  trim: function (t) {
    return t.replace(/(^\s*)|(\s*$)/g, "");
  },
  submitContent: function () {
    var that = this, e = this.trim(that.data.commentContent);
    console.log('commentContent.length', that.data.commentContent.length)
    if (e.length <= 0){
      Toast.fail('评论内容不能为空');
      return;
    }
    // return;
    fetchApi.httpPost('/orderAppraise/pubAppraise', {
      order_id: that.data.orderId,
      info_id: that.data.infoid,
      star: that.data.starNum,
      content: e
    }, function (res) {
      console.log("发布评论返回数据", res)
      if (res.data.code == 0) {
        var appraise_id = parseInt(res.data.data)
        if (appraise_id > 0 && that.data.img.uploadedImagesPaths.length > 0) {
          for (var index in that.data.img.uploadedImagesPaths) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/orderAppraise/uploadImg',
              filePath: that.data.img.uploadedImagesPaths[index].url,
              name: 'file',
              formData: { appraise_id: appraise_id },
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }
        wx.redirectTo({ url: "/pages/order/appraiseResult/result" });
      }else{
        Toast.fail(res.data.msg)
        return;
      }
    },function(){})
  },
  checkStar: function (t) {
    console.log("选择评分", t);
    var a = t.currentTarget.dataset.id || 0;
    this.setData({
      starNum: a
    });
  },
  bindinput: function (t) {
    console.log("评论输入", t)
    this.data.commentContent = t.detail.value || "";
  },
  previewImage:function(e){
    console.log('previewImage e', e)
    var a = e.currentTarget.dataset.src[0];
    console.log('uploadedImagesPaths', this.data.img.uploadedImagesPaths)
    wx.previewImage({
      current: a,
      urls: this.data.img.uploadedImagesPaths
    });
  },
  chooseImage:function(e){
    var that = this
    console.log('chooseImage e', e)
    wx.chooseImage({
      success: function (res) {
        console.log('chooseImage res', res)
        if(res.tempFilePaths.length > 0){
          for(var index in res.tempFilePaths){
            that.data.img.uploadedImagesPaths.push(res.tempFilePaths[index])
          }
          that.setData({ img: that.data.img })
        }
      },
    })
  },
  removeImg: function (e) {
    var that = this
    console.log('removeImg e', e)
    var img_index = e.currentTarget.dataset.imgnum
    that.data.img.uploadedImagesPaths.splice(img_index, 1)
    that.setData({
      img: that.data.img
    })
  }
})